import { Dialog } from '@angular/cdk/dialog';
import { Component, Input } from '@angular/core';
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatMenuModule } from '@angular/material/menu';
import { MatToolbarModule } from '@angular/material/toolbar';
import { auth } from 'ispace.core.main';
import { SettingsDialogComponent } from '../settings-dialog/settings-dialog.component';
import * as ace from 'ace-builds';
import { EditorCmpComponent } from '../editor-cmp/editor-cmp.component';
import { FileSvcService } from '../../svc/file-svc.service';
import { CommonModule } from '@angular/common';

@Component({
  selector: 'app-toolbar-cmp',
  standalone: true,
  imports: [MatToolbarModule, MatButtonModule, MatIconModule, MatButtonModule, MatMenuModule,CommonModule],
  templateUrl: './toolbar-cmp.component.html',
  styleUrl: './toolbar-cmp.component.sass'
})
export class ToolbarCmpComponent { 

  @Input() editorApi!: EditorCmpComponent;

  constructor(public dialog: Dialog,public fileSvc:FileSvcService) {
    this.initUserInfo();
  }

  isDesktop() { 
    return this.fileSvc.env.value == 'desktop'
  }

  // section user info:
  userInfo: any;
  initUserInfo() {
    this.userInfo = auth.getUserInfo();
  }

  openDialog() {
    this.dialog.open(SettingsDialogComponent, {
      minWidth: '600px',
      data: {editor:this.editorApi.editor},
    });
  }

  save() {
    this.editorApi.save();
  }

  logout() {
    auth.logout();
    window.location.reload();
  }

  replace() {
    this.editorApi.editor?.execCommand("replace", this.editorApi.editor)
  }
  find() {
    this.editorApi.editor?.execCommand("find", this.editorApi.editor)
  }


  fontSize = 12;

  RestoreBytes() {  
    this.editorApi.editor?.setFontSize(12); 
  }
  ReduceBytes() {
    let fs = Number(this.editorApi.editor?.getFontSize()); 
    if(this.fontSize == 12)
      {
        this.fontSize = fs;
      }
      if(fs<4) return;
    this.editorApi.editor?.setFontSize(fs - 4);
  }
  EnlargeBytes() {
    let fs = Number(this.editorApi.editor?.getFontSize());
    if(this.fontSize == 12)
      {
        this.fontSize =fs;
      }
    this.editorApi.editor?.setFontSize(fs + 4);
  } 
}
